let _self;
export default {
	data() {
		return {
			memberList: [],
			page: 1,
			pagesize: 9,
			searchMobile: '',
			memberId: 0,
			currentMemberInfo: null,
			endTime: '',
			sex: [{
					text: '未知',
					value: 0
				},
				{
					text: '男',
					value: 1
				},
				{
					text: '女',
					value: 2
				}
			],
			pointData: {
				num: 0,
				desc: ''
			},
			growthData: {
				num: 0,
				desc: ''
			},
			balanceData: {
				num: 0,
				desc: ''
			},
			onceCardList: [],
			onceCardData: {
				goods_id: '0',
				goods_name: ''
			},
			hongbaoCols: [{
					width: 15,
					title: '红包名称',
					align: 'left',
					field: 'name'
				},
				{
					width: 10,
					title: '类型',
					align: 'left',
					templet: function(data) {
						let config = {
							site: '店铺红包',
							platform: '平台红包'
						};
						return config[data.hongbao_type] || '';
					}
				},
				{
					width: 18,
					title: '红包金额',
					align: 'left',
					templet: function(data) {
						return '￥' + data.money;
					}
				},
				{
					width: 20,
					title: '有效期',
					align: 'center',
					templet: data => {
						var html = '开始：' + _self.$util.timeFormat(data.start_time) + '<br>';
						html += '结束：' + _self.$util.timeFormat(data.end_time) + '';
						return html;
					}
				},
				{
					width: 10,
					title: '状态',
					align: 'center',
					return: data => {
						if (data.state == 1) return '未使用';
						if (data.state == 2) return '已使用';
						if (data.state == 3) return '已过期';
					}
				},
				{
					width: 15,
					title: '来源活动',
					align: 'center',
					field: 'from_type_name'
				},
				{
					width: 15,
					title: '领取时间',
					align: 'right',
					templet: function(data) {
						return _self.$util.timeFormat(data.fetch_time);
					}
				}
			],
			couponcols: [{
					width: 15,
					title: '优惠券名称',
					align: 'left',
					field: 'coupon_name'
				},
				{
					width: 10,
					title: '类型',
					align: 'left',
					templet: function(data) {
						if (data.type == 'reward') return '满减';
						if (data.type == 'discount') return '折扣';
					}
				},
				{
					width: 18,
					title: '优惠金额',
					align: 'left',
					templet: function(data) {
						if (data.type == 'reward') {
							var html = `满${data.at_least}元减${data.money}`;
							return `<view title="${html}">${html}</view>`;
						}
						if (data.type == 'discount') {
							var text = '满' + data.at_least + '元打' + data.discount + '折';
							if (data.discount_limit) text += '（最多抵扣' + data.discount_limit + '元）';
							return '<view title="' + text + '">' + text + '</view>';
						}
					}
				},
				{
					width: 20,
					title: '有效期',
					align: 'center',
					templet: data => {
						if (data.end_time) return this.$util.timeFormat(data.end_time);
						else return '长期有效';
					}
				},
				{
					width: 20,
					title: '状态',
					align: 'center',
					return: data => {
						if (data.state == 1) return '未使用';
						if (data.state == 2) return '已使用';
						if (data.state == 3) return '已过期';
					}
				},
				{
					width: 20,
					title: '领取时间',
					align: 'right',
					templet: function(data) {
						return _self.$util.timeFormat(data.fetch_time);
					}
				}
			],
			pointCols: [{
					width: 20,
					title: '积分',
					align: 'left',
					field: 'account_data'
				},
				{
					width: 25,
					title: '发生方式',
					align: 'left',
					field: 'type_name'
				},
				{
					width: 25,
					title: '发生时间',
					align: 'left',
					templet: data => {
						var html = _self.$util.timeFormat(data.create_time);
						return html;
					}
				},
				{
					width: 30,
					title: '备注',
					align: 'left',
					field: 'remark'
				}
			],
			balanceCols: [{
					width: 10,
					title: '账户类型',
					align: 'left',
					field: 'account_type_name'
				}, {
					width: 15,
					title: '余额',
					align: 'left',
					field: 'account_data'
				},
				{
					width: 20,
					title: '发生方式',
					align: 'left',
					field: 'type_name'
				},
				{
					width: 25,
					title: '发生时间',
					align: 'left',
					templet: data => {
						var html = _self.$util.timeFormat(data.create_time);
						return html;
					}
				},
				{
					width: 30,
					title: '备注',
					align: 'left',
					field: 'remark'
				}
			],
			growthCols: [{
					width: 20,
					title: '成长值',
					align: 'left',
					field: 'account_data'
				},
				{
					width: 25,
					title: '发生方式',
					align: 'left',
					field: 'type_name'
				},
				{
					width: 25,
					title: '发生时间',
					align: 'left',
					templet: data => {
						var html = _self.$util.timeFormat(data.create_time);
						return html;
					}
				},
				{
					width: 30,
					title: '备注',
					align: 'left',
					field: 'remark'
				}
			],
			option: {},
			addMemberData: {
				mobile: '',
				nickname: '',
				sex: 0,
				birthday: ''
			},
			// 第一次请求列表、详情渲染判断
			one_judge: true,
			refundTransferTypeList: {},
			refundTransferType: '',
			refundData: null,
			isRepeat: false
		};
	},
	onLoad(options) {
		_self = this;
		this.getMemberList();
		let date = new Date();
		var y = date.getFullYear();
		var m = date.getMonth() + 1;
		var d = date.getDate();
		this.endTime = y + '-' + m + '-' + d;
	},
	methods: {
		// 查询客户列表
		getMemberList(member_id) {
			let data = {
				page: this.page,
				page_size: 20,
				search_text: this.searchMobile,
			};
			if (member_id) data.member_id = member_id
			this.$api.sendRequest({
				url: '/cashier/storeapi/member/lists',
				data: data,
				success: res => {
					if (res.code >= 0) {
						if (this.page == 1) this.memberList = [];
						this.memberList = this.memberList.concat(res.data.list);
						this.memberList.forEach((item) => {
							if (item.mobile) {
								if (this.userInfo.is_admin == 0) {
									// 非管理员，不能查看会员手机号
									item.mobile = item.mobile.substring(0, 4 - 1) +
										'****' + item.mobile.substring(6 + 1);
								}
							} else {
								item.mobile = '';
							}
						})
						if (this.page == 1 && this.memberList.length > 0) {
							// 订单页面跳转过来查看 会员详情
							let orderMemberId = uni.getStorageSync('member_id');
							if (orderMemberId) {
								// 清除传过来的member_id
								setTimeout(() => {
									uni.removeStorageSync('member_id');
								}, 2000);
							}
							this.memberId = orderMemberId || this.memberList[0]['member_id'];
							this.getMemberInfo();
						} else {
							this.one_judge = false;
							// this.memberId = 0;
						}
						if (res.data.page_count >= this.page) this.page++;
					}
				}
			});
		},
		searchMember() {
			this.page = 1;
			this.one_judge = true;
			this.getMemberList();
		},
		selectMember(member_id) {
			this.memberId = member_id;
			this.getMemberInfo();
		},
		getMemberInfo() {
			this.$api.sendRequest({
				url: '/cashier/storeapi/member/info',
				data: {
					member_id: this.memberId
				},
				success: res => {
					if (res.code >= 0) {
						this.currentMemberInfo = res.data;
						this.currentMemberInfo.birthday = res.data.birthday > 0 ? this.$util.timeFormat(res
							.data
							.birthday, 'y-m-d') : '';
						this.one_judge = false;
					}
				}
			});
		},
		verify() {
			if (!this.addMemberData.mobile) {
				this.$util.showToast({
					title: '请输入客户手机号'
				});
				return false;
			}
			if (!this.$util.verifyMobile(this.addMemberData.mobile)) {
				this.$util.showToast({
					title: '请输入正确的手机号码'
				});
				return false;
			}

			return true;
		},
		// 添加客户
		addMember() {
			if (this.verify()) {
				if (this.flag) return;
				this.flag = true;
				this.$api.sendRequest({
					url: '/cashier/storeapi/member/addmember',
					data: this.addMemberData,
					success: res => {
						if (res.code == 0 && res.data) {
							this.addMemberData = {
								mobile: '',
								nickname: '',
								sex: 0,
								birthday: ''
							};
							this.page = 1;
							this.getMemberList();
							this.popClose('addmember');
						} else {
							this.$util.showToast({
								title: '该手机号已注册为客户'
							});
						}
						this.flag = false;
					}
				});
			}
		},
		// 客户操作
		memberAction(type) {
			switch (type) {
				case 'addmember':
					this.$refs.addmemberPop.open();
					break;
			}
		},
		popClose(type) {
			switch (type) {
				case 'addmember':
					this.$refs.addmemberPop.close();
					break;
			}
		},
		refundBalance() {
			this.$api.sendRequest({
				url: '/cashier/storeapi/cashierorderrefund/getMemberBalanceRefundData',
				data: {
					member_id: this.currentMemberInfo.member_id
				},
				success: res => {
					if (res.code == 0) {
						this.refundData = res.data;
						this.refundData.refund_balance = this.$util.moneyFormat(res.data
							.can_refund_balance);
						this.refundTransferTypeList = res.data.refund_transfer_type_list;
						this.refundTransferType = Object.keys(res.data.refund_transfer_type_list)[0];
						this.$refs.refundBalance.open();
					} else {
						this.$util.showToast({
							title: res.message
						})
					}
				}
			})
		},
		confirmRefundBalance() {
			if (isNaN(parseFloat(this.refundData.refund_balance))) {
				this.$util.showToast({
					title: '退还金额格式错误'
				})
				return;
			}
			if (parseFloat(this.refundData.refund_balance) > parseFloat(this.refundData.can_refund_balance)) {
				this.$util.showToast({
					title: '退还金额超出最大可退金额'
				})
				return;
			}
			if (this.refundData.refund_balance <= 0) {
				this.$util.showToast({
					title: '退还金额不能小于等于0'
				})
				return;
			}

			if (this.isRepeat) return;
			this.isRepeat = true;

			this.$api.sendRequest({
				url: '/cashier/storeapi/cashierorderrefund/memberRefundBalance',
				data: {
					refund_balance: this.$util.moneyFormat(this.refundData.refund_balance),
					member_id: this.currentMemberInfo.member_id,
					refund_transfer_type: this.refundTransferType
				},
				success: res => {
					if (res.code == 0) {
						this.$util.showToast({
							title: '储值退还成功'
						})
						this.getMemberInfo();
						this.$refs.refundBalance.close();
					} else {
						this.$util.showToast({
							title: res.message
						})
					}
					this.isRepeat = true;
				}
			})
		},
		headError(item) {
			item.headimg = this.defaultImg.head;
		}
	}
}